package superConference;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * A very rough draft of the author class.
 * 
 * @author Gian Bata
 * @version 1.0v
 */
public class Author extends AbstractUser {

	/**
	 * The list of all papers.
	 */
	private List<Paper> my_list_of_papers;

	/**
	 * Constructs an author.
	 * 
	 * @param the_id
	 *            The user ID of the author.
	 * @param the_email
	 *            The email address if the author.
	 * @param the_name
	 *            The name of the author.
	 */
	public Author(int the_id, String the_email, String the_name) {
		super(the_id, the_email, the_name);

		my_list_of_papers = new ArrayList<Paper>();
	}

	/**
	 * Constructs an author.
	 * 
	 * @param the_id
	 *            The user ID of the author.
	 * @param the_email
	 *            The email address if the author.
	 * @param the_name
	 *            The name of the author.
	 */
	public Author(int the_id, String the_email, String the_name,
			List<Paper> the_listOfPapers) {
		super(the_id, the_email, the_name);

		this.my_list_of_papers = the_listOfPapers;
	}

	/**
	 * Add a paper to the list of papers.
	 * @param paper a paper
	 */
	public void addPaper(Paper paper) {
		my_list_of_papers.add(paper);
	}
	
	/**
	 * Submits the current paper.
	 */
	public void submitPaper(File the_file) {
		File fileToMakePaper = the_file;
		Paper paperToBeSubmitted = createPaper(fileToMakePaper);

		my_list_of_papers.add(paperToBeSubmitted);
	}

	/**
	 * Creates a paper.
	 * 
	 * @param the_file
	 * @return the created paper file.
	 */
	public Paper createPaper(File the_file) {
		File file = the_file;
		String title = the_file.getName();

		Paper paper = new Paper(this, title, file);

		return paper;
	}

	/**
	 * Sets the current paper via GUI that this class is going to use, then
	 * opens the current paper to be viewed in the GUI.
	 * 
	 * @param the_paper
	 * @throws FileNotFoundException
	 */
	public String openPaper(File the_paper) throws FileNotFoundException {
		File paper_file = the_paper;
		Scanner scanner = new Scanner(paper_file);
		StringBuilder sb = new StringBuilder();

		while (scanner.hasNextLine()) {
			sb.append(scanner.nextLine() + "\n");
		}

		scanner.close();
		return sb.toString();
	}

	/**
	 * Overwrites the old version (the current opened paper) of the paper to a
	 * new one.
	 * 
	 * @param the_new_version_paper
	 *            The new version of the paper that will replace the
	 *            old(current) paper.
	 */
	public String updatePaper(File the_new_version_paper) {
		Paper paperUpdate = createPaper(the_new_version_paper);
		StringBuilder sb = new StringBuilder();
		
		my_list_of_papers.add(paperUpdate);
		
		for (int i = 0; i < my_list_of_papers.size(); i++) {
			if (paperUpdate.getTitle().equals(my_list_of_papers.get(i).getTitle())) {
				my_list_of_papers.remove(i);
				break;
			}
		}
		
		sb.append("The old " + paperUpdate.getTitle() + " has been replaced with a new one. \n\n");
		sb.append("Updates list of papers: \n");
	
		String listOfPapers = visewListOfPapers();
		sb.append(listOfPapers);
		
		return sb.toString();
	}

	/**
	 * Returns a string representation list of all papers including their names
	 * and ids.
	 * 
	 * @return a string representation.
	 */
	public String visewListOfPapers() {
		if (my_list_of_papers.isEmpty()) {
			return "There are no papers submitted";
		} else {
			Iterator<Paper> iterator = my_list_of_papers.iterator();

			StringBuilder sb = new StringBuilder();

			sb.append("ID \t Title");
			sb.append("\n");

			while (iterator.hasNext()) {
				Paper x = iterator.next();

				sb.append(x.getId() + "\t" + x.getTitle());
				sb.append("\n");
			}

			return sb.toString();
		}
	}

	/**
	 * Remove unwanted papers.
	 * 
	 * @param the_paper
	 */
	public void unsubmitPaper(Paper the_paper) {
		my_list_of_papers.remove(the_paper);
	}

	public void unsubmitPaper(String the_title) {

		for (int i = 0; i < my_list_of_papers.size(); i++) {
			if (the_title.equals(my_list_of_papers.get(i).getTitle())) {
				my_list_of_papers.remove(i);
				break;
			}
		}

	}

	public void unsubmitPaper(int the_id) {

		for (int i = 0; i < my_list_of_papers.size(); i++) {
			if (the_id == my_list_of_papers.get(i).getId()) {
				my_list_of_papers.remove(i);
				break;
			}
		}
	}

	/**
	 * Returns a list of all papers the author submitted.
	 * 
	 * @return the list of papers.
	 */
	public List<Paper> getListOfPaper() {
		return my_list_of_papers;
	}
	

	/**
	 * The paper with title x.
	 * 
	 * @param the_paper_title
	 *            the name/title of the paper.
	 * @return the title of the paper.
	 */
	public Paper getPaper(String the_paper_title) {
		Paper paper_to_be_returned = null;

		for (int i = 0; i < my_list_of_papers.size(); i++) {
			Paper temp_paper = my_list_of_papers.get(i);

			if (temp_paper.getTitle() == the_paper_title) {
				paper_to_be_returned = temp_paper;
				break; // Stops the search for the paper.
			} else {
				paper_to_be_returned = null;
			}

		}

		return paper_to_be_returned;
	}
	
	public String getReviews() throws NullPointerException {
		StringBuilder sb = new StringBuilder();
		
		for (Paper x : my_list_of_papers) {
			List<Review> listOfReviews = x.getReviews();
			
			if (!listOfReviews.isEmpty()) {
				sb.append("The reviews for " + x.getTitle() + " are: \n");
				
				for (Review y : listOfReviews) {
					sb.append(y.getFileName() + "\n");	
				}
				
				sb.append("\n");
				
			} else {
				sb.append("The paper with title " + x.getTitle() + " has no reviews yet. \n");
			}
		}
		
		return sb.toString();
	}
	

	public String getName() {
		return this.name;
	}

	public String getEmail() {
		return this.email;
	}

	public int getId() {
		return this.userID;
	}

}
